#ifndef GAMECONTROL_H
#define GAMECONTROL_H

#include <QObject>
#include "robot.h"
#include "userplayer.h"
#include "cards.h"

struct BetRecord
{
    BetRecord()
    {
        reset();
    }
    void reset()
    {
        player = nullptr;
        bet = 0;
        times = 0;
    }
    Player* player;
    int bet;
    int times;  // 第几次叫地主
};

class GameControl : public QObject
{
    Q_OBJECT
public:
    //游戏状态
    enum GameStatus
    {
        DispatchCard, //发牌状态
        CallingLord,  //叫地主状态
        PlayingHand  //出牌状态
    };
    //玩家状态
    enum PlayerStatus
    {
        ThinkingForCallLord,  //考虑叫地主
        ThinkingForPlayHand,  //考虑出牌
        Winning  //获胜
    };

    explicit GameControl(QObject *parent = nullptr);

    //初始化玩家
    void playerInit();

    //得到玩家的实例对象
    Robot* getLeftRobot();
    Robot* getRightRobot();
    UserPlayer* getUserPlayer();

    //设置/获取当前玩家
    void setCurrentPlayer(Player* player);
    Player* getCurrentPlayer();

    //得到出牌玩家和打出的牌
    Player* getPendPlayer();
    Cards getPendCards();

    //初始化扑克牌
    void initAllCards();
    //每次发一张牌
    Card takeOneCard();
    //得到最后的三张底牌
    Cards getSurplusCards();
    //重置卡牌数据
    void resetCardData();

    //准备叫地主
    void startLordCard();
    //成为地主
    void becomeLord(Player *player, int bet);
    //清空所有玩家的得分
    void clearPlayerScore();
    // 得到玩家下注的最高分数
    int getPlayerMaxBet();


    // 处理叫地主
    void onGrabBet(Player* player, int bet);


    //处理出牌
    void onPlayHand(Player *player, const Cards &card);

signals:

    void playerStatusChanged(Player* player, PlayerStatus status);
    // 通知玩家抢地主了
    void notifyGrabLordBet(Player* player, int bet, bool flag);
    // 游戏状态变化
    void gameStatusChanged(GameStatus status);
    // 通知玩家出牌了
    void notifyPlayHand(Player* player, const Cards& card);
    // 给玩家传递出牌数据
    void pendingInfo(Player* player, const Cards& card);
private:
    Robot* m_robotLeft = nullptr;  //左边玩家
    Robot* m_robotRight = nullptr; //右边玩家
    UserPlayer* m_user = nullptr;  //用户玩家
    Player* m_currPlayer = nullptr; //当前玩家
    Player* m_pendPlayer = nullptr; //出牌玩家
    Cards m_pendCards;  //玩家打出的牌
    Cards m_allCards;
    BetRecord m_betRecord; //记录玩家抢地主的下注
    int m_curBet = 0; //底分
};

#endif // GAMECONTROL_H
